#include "jsonhandle.h"

JsonHandle::JsonHandle(QObject *parent) : QObject(parent)
{

}

void JsonHandle::savePathPrefix(QJsonObject &format_obj, Format *format)
{
    QJsonObject path_prefix;
    path_prefix.insert("file", format->path_refix.file);
    path_prefix.insert("url", format->path_refix.url);
    format_obj.insert("path_prefix", path_prefix);
}

void JsonHandle::saveVideoStreams(QJsonObject &format_obj, Format *format)
{
    QJsonArray video_streams;
    for (int i = 0; i < format->video_streams.size(); i++)
    {
        QJsonObject video_stream_obj;
        video_stream_obj.insert("stream_destination", format->video_streams.at(i).stream_destination);
        QJsonArray lives;
        for (int j = 0; j < format->video_streams.at(i).lives.size(); j++)
        {
            QJsonObject live_obj;
            live_obj.insert("interface", format->video_streams.at(i).lives.at(j).interface);
            live_obj.insert("ip", format->video_streams.at(i).lives.at(j).ip);
            live_obj.insert("port", format->video_streams.at(i).lives.at(j).port);
            lives.insert(j, live_obj);
        }
        video_stream_obj.insert("live", lives);
        video_stream_obj.insert("local", format->video_streams.at(i).local);
        video_streams.insert(i, video_stream_obj);
    }
    format_obj.insert("video_streams", video_streams);
}

void JsonHandle::saveArchitectureVersion(QJsonObject &format_obj, Format *format)
{
    format_obj.insert("architecture_version", format->architecture_version);
}

void JsonHandle::saveCurrentShow(QJsonObject &format_obj, Format *format)
{
    format_obj.insert("current_show", format->current_show);
}

void JsonHandle::saveResolution(QJsonObject &format_obj, Format *format)
{
    QJsonObject resolution;
    resolution.insert("width", format->resolution.width);
    resolution.insert("height", format->resolution.height);
    format_obj.insert("resolution", resolution);
}

void JsonHandle::saveResources(QJsonObject &format_obj, Format *format)
{
    QJsonObject resources;
    for (auto iter = format->resources.begin(); iter != format->resources.end(); iter++)
        resources.insert(iter.key(), iter.value());
    format_obj.insert("resources", resources);
}

void JsonHandle::saveScreens(QJsonObject &format_obj, Format *format)
{
    QJsonArray screens;
    int i_index = 0;
    for (auto i = format->screens.begin(); i != format->screens.end(); i++)
    {
        Screen one_screen = i.value();
        QJsonObject screen_obj;
        screen_obj.insert("id", one_screen.id);
        screen_obj.insert("name", one_screen.name);

        QJsonArray partitions;
        int j_index = 0;
        for (auto j = one_screen.partitions.begin(); j != one_screen.partitions.end(); j++)
        {
            Partition one_partition = j.value();
            QJsonObject partition_obj;
            partition_obj.insert("id", one_partition.id);
            QJsonObject common_obj;
            common_obj.insert("x", one_partition.common.x);
            common_obj.insert("y", one_partition.common.y);
            common_obj.insert("width", one_partition.common.width);
            common_obj.insert("height", one_partition.common.height);
            common_obj.insert("z_order", one_partition.common.z_order);
            common_obj.insert("transparent", one_partition.common.transparent);
            common_obj.insert("background", one_partition.common.background);
            common_obj.insert("background_color", one_partition.common.background_color);
            partition_obj.insert("common", common_obj);

            QJsonArray layouts;
            int k_index = 0;
            for (auto k = one_partition.layouts.begin(); k != one_partition.layouts.end(); k++)
            {
                Layout one_layout = k.value();
                QJsonObject layout_obj;
                layout_obj.insert("id", one_layout.id);
                layout_obj.insert("duration", one_layout.duration);

                QJsonArray items;
                int l_index = 0;
                for (int l = 0; l < one_layout.items.size(); l++)
                {
                    Item one_item = one_layout.items.at(l);
                    QJsonObject item_obj;
                    for (auto n = one_item.cabpositems.begin(); n != one_item.cabpositems.end(); n++)
                    {
                        QJsonObject cab_pos_item_obj;
                        CabPosItem one_cab_pos_item = n.value();
                        cab_pos_item_obj.insert("id", one_cab_pos_item.id);
                        cab_pos_item_obj.insert("type", one_cab_pos_item.type);
                        cab_pos_item_obj.insert("x", one_cab_pos_item.x);
                        cab_pos_item_obj.insert("y", one_cab_pos_item.y);
                        cab_pos_item_obj.insert("width", one_cab_pos_item.width);
                        cab_pos_item_obj.insert("height", one_cab_pos_item.height);
                        if (one_cab_pos_item.type == "text")
                        {
                            cab_pos_item_obj.insert("font", one_cab_pos_item.font);
                            cab_pos_item_obj.insert("size", one_cab_pos_item.size);
                            cab_pos_item_obj.insert("bold", one_cab_pos_item.bold);
                            cab_pos_item_obj.insert("italic", one_cab_pos_item.italic);
                            cab_pos_item_obj.insert("fore_color", one_cab_pos_item.fore_color);
                            cab_pos_item_obj.insert("back_color", one_cab_pos_item.back_color);
                            cab_pos_item_obj.insert("background", one_cab_pos_item.background);
                            cab_pos_item_obj.insert("background_color", one_cab_pos_item.background_color);
                            cab_pos_item_obj.insert("align", one_cab_pos_item.align);
                            cab_pos_item_obj.insert("effect", one_cab_pos_item.effect);
                            cab_pos_item_obj.insert("speed", one_cab_pos_item.speed);
                        }
                        if (one_cab_pos_item.type == "video")
                            cab_pos_item_obj.insert("sound", one_cab_pos_item.sound);

                        QJsonObject data_obj;

                        if (one_cab_pos_item.type != "video")
                        {
                            QJsonArray materials;
                            int m_index = 0;
                            for (auto m = one_cab_pos_item.data.materials.begin(); m != one_cab_pos_item.data.materials.end(); m++)
                            {
                                Material one_material = m.value();
                                QJsonObject material_obj;
                                material_obj.insert("duration", one_material.duration);

                                material_obj.insert("content", one_material.content);

                                materials.insert(m_index++, material_obj);
                            }
                            data_obj.insert("materials", materials);

                        } else
                        {
                            QJsonObject video_stream_obj;
                            video_stream_obj.insert("all", one_cab_pos_item.data.stream_index.all);
                            data_obj.insert("stream_index", video_stream_obj);
                        }

                        cab_pos_item_obj.insert("data", data_obj);
                        item_obj.insert(n.key(), cab_pos_item_obj);
                    }
                    if (item_obj.size() != 0)
                        items.insert(l_index++, item_obj);
                }

                if (items.size() != 0)
                    layout_obj.insert("items", items);
                layouts.insert(k_index++, layout_obj);
            }
            if (layouts.size() != 0)
                partition_obj.insert("layouts", layouts);
            partitions.insert(j_index++, partition_obj);
        }

        if (partitions.size() != 0)
            screen_obj.insert("partitions", partitions);
        screens.insert(i_index++, screen_obj);
    }
    if (screens.size() != 0)
        format_obj.insert("screens", screens);
}

//保存，数据结构转化成json文件
void JsonHandle::saveFormatData(Format *format, const QString &path)
{
    QJsonObject format_obj;

    savePathPrefix(format_obj, format);
    saveVideoStreams(format_obj, format);
    saveArchitectureVersion(format_obj, format);
    saveCurrentShow(format_obj, format);
    saveResolution(format_obj, format);
    saveResources(format_obj, format);
    saveScreens(format_obj, format);

    QJsonObject root_obj;
    if (format->type == "LCD")
        root_obj.insert("format_lcd", format_obj);
    else
        root_obj.insert("format_idu", format_obj);

    QJsonDocument root_doc;
    root_doc.setObject(root_obj);
    QByteArray root_str = root_doc.toJson(QJsonDocument::Indented);  //紧凑格式

    QString text = QString(root_str);

    if (text.contains("\"(#current_show#)\""))
        text.replace("\"(#current_show#)\"", "(#current_show#)");
    if (text.contains("\"(#playlist1#)\""))
        text.replace("\"(#playlist1#)\"", "(#playlist1#)");

    QFile file(path);
    if (!file.open(QIODevice::WriteOnly))
    {
        qDebug() << "File open error";
        return;
    }

    file.write(text.toUtf8());
    file.close();
}

//代理创建,json文件转化成数据结构
Format* JsonHandle::createFormatData(const QString &path)
{
    Format *format = NULL;

    QByteArray ba;
    QFile file(path);
    if (file.open(QIODevice::ReadOnly))
        ba = file.readAll();
    file.close();

    QString text = QString(ba);
    if (text.contains("(#current_show#)"))
        text.replace("(#current_show#)", "\"(#current_show#)\"");
    if (text.contains("(#playlist1#)"))
        text.replace("(#playlist1#)", "\"(#playlist1#)\"");

    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(text.toUtf8(), &jsonError);
    if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
    {
        format = new Format();
        if (doucment.isObject())
        {
            QJsonObject root = doucment.object();
            if (root.contains("format_lcd") || root.contains("format_idu"))
            {
                QJsonObject root_obj = root.value("format_lcd").toObject();
                if (root.contains("format_idu"))
                {
                    root_obj = root.value("format_idu").toObject();
                    format->type = "IDU";
                }
                getPathPrefix(format, root_obj);
                getVideoStreams(format, root_obj);
                getArchitectureVersion(format, root_obj);
                getCurrentShow(format, root_obj);
                getResolution(format, root_obj);
                getResources(format, root_obj);
                getScreens(format, root_obj);
            }
        }
    } else
    {
        qDebug() << jsonError.errorString();
        qDebug() << jsonError.offset;
    }
    return format;
}

void JsonHandle::getPathPrefix(Format *format, QJsonObject root_obj)
{
    if (root_obj.contains("path_prefix"))
    {
        QJsonObject path_prefix_obj = root_obj.value("path_prefix").toObject();
        if (path_prefix_obj.contains("file"))
        {
            QString file = path_prefix_obj.value("file").toString();
            format->path_refix.file = file;
        }
        if (path_prefix_obj.contains("url"))
        {
            QString url = path_prefix_obj.value("url").toString();
            format->path_refix.url = url;
        }
    }
}

void JsonHandle::getVideoStreams(Format *format, QJsonObject root_obj)
{
    if (root_obj.contains("video_streams"))
    {
        VideoStream one_stream;

        QJsonArray video_streams_array = root_obj.value("video_streams").toArray();
        for (int i = 0; i < video_streams_array.size(); i++)
        {
            QJsonObject video_stream_obj = video_streams_array.at(i).toObject();

            int stream_destination = video_stream_obj.value("stream_destination").toInt();
            QString local = video_stream_obj.value("local").toString();

            QJsonArray lives = video_stream_obj.value("live").toArray();
            for (int j = 0; j < lives.size(); j++)
            {
                QJsonObject live_obj = lives.at(j).toObject();
                Live one_live;
                one_live.interface = live_obj.value("interface").toInt();
                if (one_live.interface == 0)
                    one_live.interface = 1;
                one_live.ip = live_obj.value("ip").toString();
                one_live.port = live_obj.value("port").toInt();
                one_stream.lives.push_back(one_live);
            }

            one_stream.stream_destination = stream_destination;
            one_stream.local              = local;
            format->video_streams.push_back(one_stream);
        }
    }
}

void JsonHandle::getArchitectureVersion(Format *format, QJsonObject root_obj)
{
    if (root_obj.contains("architecture_version"))
    {
        QString architecture_version = root_obj.value("architecture_version").toString();
        format->architecture_version = architecture_version;
    }
}

void JsonHandle::getCurrentShow(Format *format, QJsonObject root_obj)
{
    if (root_obj.contains("current_show"))
    {
        QString current_show = root_obj.value("current_show").toString();
        format->current_show = current_show;
    }
}

void JsonHandle::getResolution(Format *format, QJsonObject root_obj)
{
    if (root_obj.contains("resolution"))
    {
        QJsonObject resolution_obj = root_obj.value("resolution").toObject();
        if (resolution_obj.contains("width"))
        {
            int width = resolution_obj.value("width").toInt();
            format->resolution.width = width;
        }
        if (resolution_obj.contains("height"))
        {
            int height = resolution_obj.value("height").toInt();
            format->resolution.height = height;
        }
    }
}

void JsonHandle::getResources(Format *format, QJsonObject root_obj)
{
    if (root_obj.contains("resources"))
    {
        QJsonObject resources = root_obj.value("resources").toObject();
        foreach(const QString& key, resources.keys())
            format->resources[key] = resources.value(key).toString();
    }
}

void JsonHandle::getScreens(Format *format, QJsonObject root_obj)
{
    if (root_obj.contains("screens"))
    {
        QJsonArray screens_array = root_obj.value("screens").toArray();

        for (int i = 0; i < screens_array.size(); i++)
        {
            QJsonObject screen_obj = screens_array.at(i).toObject();

            Screen one_screen;
            one_screen.id = screen_obj.value("id").toInt();
            one_screen.name = screen_obj.value("name").toString();

            QJsonArray partitions_array = screen_obj.value("partitions").toArray();
            for (int j = 0; j < partitions_array.size(); j++)
            {
                QJsonObject partition_obj = partitions_array.at(j).toObject();
                Partition one_partition;
                one_partition.id = partition_obj.value("id").toInt();

                QJsonObject common_obj = partition_obj.value("common").toObject();
                Common common;
                common.x = common_obj.value("x").toInt();
                common.y = common_obj.value("y").toInt();
                common.width = common_obj.value("width").toInt();
                common.height = common_obj.value("height").toInt();
                common.z_order = common_obj.value("z_order").toInt();
                common.background = common_obj.value("background").toString();
                if (common_obj.contains("background_color"))
                    common.background_color = common_obj.value("background_color").toString();
                common.transparent = common_obj.value("transparent").toBool();
                one_partition.common = common;

                QJsonArray layouts_array = partition_obj.value("layouts").toArray();
                for (int k = 0; k < layouts_array.size(); k++)
                {
                    QJsonObject layout_obj = layouts_array.at(k).toObject();

                    Layout one_layout;
                    one_layout.id = layout_obj.value("id").toInt();
                    one_layout.duration = layout_obj.value("duration").toInt();

                    QJsonArray items_array = layout_obj.value("items").toArray();
                    for (int n = 0; n < items_array.size(); n++)
                    {
                        QJsonObject cab_pos_item_obj = items_array.at(n).toObject();
                        Item one_item;
                        one_item.id = n+1;
                        for (int l = 0; l < cab_pos_item_obj.size(); l++)
                        {
                            QStringList keys = cab_pos_item_obj.keys();
                            QJsonObject item_obj = cab_pos_item_obj.value(keys.at(l)).toObject();
                            CabPosItem one_cab_pos_item;
                            one_cab_pos_item.id = item_obj.value("id").toInt();
                            one_cab_pos_item.type = item_obj.value("type").toString();
                            one_cab_pos_item.x = item_obj.value("x").toInt();
                            one_cab_pos_item.y = item_obj.value("y").toInt();
                            one_cab_pos_item.width = item_obj.value("width").toInt();
                            one_cab_pos_item.height = item_obj.value("height").toInt();
                            if (one_cab_pos_item.type == "text")
                            {
                                one_cab_pos_item.font = item_obj.value("font").toString();
                                one_cab_pos_item.size = item_obj.value("size").toInt();
                                one_cab_pos_item.bold = item_obj.value("bold").toBool();
                                one_cab_pos_item.italic = item_obj.value("italic").toBool();
                                one_cab_pos_item.fore_color = item_obj.value("fore_color").toString();
                                one_cab_pos_item.back_color = item_obj.value("back_color").toString();
                                one_cab_pos_item.background = item_obj.value("background").toString();
                                if (item_obj.contains("background_color"))
                                    one_cab_pos_item.background_color = item_obj.value("background_color").toString();
                                one_cab_pos_item.align = item_obj.value("align").toInt();
                                one_cab_pos_item.effect = item_obj.value("effect").toInt();
                                one_cab_pos_item.speed = item_obj.value("speed").toInt();
                            }
                            if (one_cab_pos_item.type == "video")
                                one_cab_pos_item.sound = item_obj.value("sound").toBool();

                            QJsonObject data_obj = item_obj.value("data").toObject();
                            Data data;
                            if (one_cab_pos_item.type != "video")
                            {
                                QJsonArray materials_array = data_obj.value("materials").toArray();
                                for (int m = 0; m < materials_array.size(); m++)
                                {
                                    QJsonObject material_obj = materials_array.at(m).toObject();
                                    Material one_material;
                                    one_material.duration = material_obj.value("duration").toInt();
                                    one_material.content = material_obj.value("content").toString();
                                    data.materials[m+1] = one_material;
                                }
                            } else
                            {
                                QJsonObject stream_index_obj = data_obj.value("stream_index").toObject();
                                data.stream_index.all = stream_index_obj.value("all").toInt();
                            }

                            one_cab_pos_item.data = data;
                            one_item.cabpositems[keys.at(l)] = one_cab_pos_item;
                        }
                        one_layout.items.push_back(one_item);
                    }
                    one_partition.layouts[one_layout.id] = one_layout;
                }
                one_screen.partitions[one_partition.id] = one_partition;
            }
            format->screens[one_screen.id] = one_screen;
        }
    }
}

















